/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 * Display any thing dynamic (e.g.: animation of lines, moving axis, hightlighted lines ...)
 *
 * @author: jin chen
 * Date: Jun 10, 2003
 * Time: 2:20:33 PM
 */
package edu.psu.geovista.app.pcp.plot.layer;

import edu.psu.geovista.app.pcp.plot.layer.operation.BasicLayerOp;
import edu.psu.geovista.app.pcp.plot.layer.operation.DrawLineOp;
import edu.psu.geovista.app.pcp.plot.layer.operation.HighLightLineOp;
import edu.psu.geovista.app.pcp.plot.layer.operation.PreviewAxisOp;
import edu.psu.geovista.app.plot.axis.YAxis;
import edu.psu.geovista.app.plot.layer.Layer;

import java.awt.*;
import java.util.Hashtable;

public class DynamicLayer extends RuntimeLayer{
    BasicLayerOp operator;
    Hashtable operators;

    public DynamicLayer(String name) {
        this.setName(name);
        this.setPersisted(false);
        setupOperator();
        //this.setLevel(Layer.TOP) ;

    }
   
    /**
     * In Layer.PAINT_MODE_SIMPLE mode, not paint record label
     * @param mode
     */
    public void setPaintMode(int mode){

        HighLightLineOp hl=(HighLightLineOp) operators.get(BasicLayerOp.Operator_Highlight);
        if(mode==Layer.PAINT_MODE_SIMPLE){

            hl.setDrawLabel(false);
        }
        else if(mode==Layer.PAINT_MODE_FULL){
            hl.setDrawLabel(true);
        }
    }
    /**
     *  Add operator here
     */
    private  void setupOperator() {
        Object[] opts={
            BasicLayerOp.Operator_PreviewAxis,new PreviewAxisOp(),
            BasicLayerOp.Operator_DrawLine,new DrawLineOp(),
            BasicLayerOp.Operator_Highlight,new HighLightLineOp(this)
        } ;

        this.operators =new Hashtable();
        for(int i = 0; i < opts.length; i += 2) {
            Object value = opts[i + 1];
            if (value == null) {
                this.operators.remove(opts[i]);
            }
            else {
                this.operators.put(opts[i], value);
            }
        }
    }
    /*******************************************************************************************************
     *                Paint functions
     *******************************************************************************************************/
    /**
     *  For ordering axis by dragging. Paint a dummy axis
     * @param axis
     * @param from
     * @param to       x location
     */
    public void  previewMoveAxis(YAxis axis, int from, int to) {
        //this.clearLayer() ;
        //this.image =null;
        PreviewAxisOp pa=(PreviewAxisOp)this.operators.get("PreviewAxisOp");
        pa.setAxis(axis);
        pa.setLocation(to,Double.NaN);//y depend on axis's y
        this.setOperator(pa) ;

    }
    /**
     * Paint brushing range
     * @param from start of brushing
     * @param to   end of brushing
     */
    public void drawBrushing(Point from, Point to) {
        DrawLineOp dl=(DrawLineOp) this.operators.get("DrawLineOp");
        dl.drawLine(from, to);
        this.setOperator(dl);

        Graphics2D g2d = (Graphics2D) this.ui.getPlotCanvas().getGraphics();
        dl.paint(g2d);



    }

    private  void setOperator(BasicLayerOp operator) {
        this.operator = operator;
    }

    /**
     * 
     * @param rowName
     * @param id
     * @param x
     * @param y
     */
    public void  highlightLine(String rowName,int id,int x, int y){
        HighLightLineOp hll=(HighLightLineOp)this.operators.get("HighLightLineOp");
        hll.setRecordId(id);
        hll.setName(rowName);
        hll.setX(x);
        hll.setY(y);
        ConfigInfo cfg=this.getConfigInfo() ;
        Color color=(Color) cfg.getConfig(ConfigInfo.Attr_COLOR );
        if(color!=null)
            hll.setColor(color);
        this.setOperator(hll);


    }

    /**
     *
     * @param g
     */
    public void paintComponent(Graphics g){
        if (this.operator !=null)
            operator.paint(g);
        //this.repaint() ;
    }
    /*
     * Use it super method, otherwise, it will create a new Image each time you brush
    public void clearLayer(){
        this.image = null;

    }*/
    public void reset() {
        //this.clearLayer() ;
        if (operator!=null) { //if just press and release
            this.operator.reset();
        }
        this.setOperator(null);


    }

}
